/**
 *
 * author ThuanLT
 * Aug 20, 2014 8:05:04 PM
 *
 */
package vn.fsoft.gst.dao;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import vn.fsoft.gst.domain.Organisation;

@Repository
public class OrganisationDAOImpl implements OrganisationDAO{

	//autowired sessionFactory
	@Autowired
	private SessionFactory sessionFactory;
	@Transactional
	public int addOrganisation(Organisation org) {
		return (Integer) sessionFactory.getCurrentSession().save(org);
		
	}
    @Transactional
	public void updateOrganisation(Organisation org) {
		sessionFactory.getCurrentSession().merge(org);
	}
    @Transactional
	public void deleteOrganisation(int orgID) {
		Organisation c = getOrganisationByID(orgID);
		sessionFactory.getCurrentSession().delete(c);
	}
    @Transactional
    @SuppressWarnings("unchecked")
	public List<Organisation> getListOrganisation() {
		//get current session and then create query to get all organisation
    	return sessionFactory.getCurrentSession().createQuery("from Organisation").list();
	}
    @Transactional
	public Organisation getOrganisationByID(int orgID) {
		return (Organisation) sessionFactory.getCurrentSession().get(Organisation.class, orgID);
	}
	
	public void activeOrganisation(int orgID) {	
		//get current session 
		Session session = sessionFactory.getCurrentSession();
		//get organisation with identify id
		Organisation org = (Organisation) session.get(Organisation.class, orgID);
		//change active property
		org.setActive(true);
		//update organisation
		session.update(org);
	}
	public int getOrganisationIDMax() {
		Session session = sessionFactory.getCurrentSession();
		int maxID = (Integer) session.createQuery("Select MAX(orgID) from Organisation ").list().get(0);
		return maxID;
	}
	public void inactiveOrganisation(int orgID) {
		Session session = sessionFactory.getCurrentSession();
		Organisation org = (Organisation) session.get(Organisation.class, orgID);
		org.setActive(false);
		session.merge(org);
		
	}

	
}
